package com.wufun.overseas.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wufun.baselibrary.download.UtilsMy;
import com.wufun.overseas.R;


/**
 * Created by xiongw on 2016/5/12.
 * 加载中页面布局
 */
public class LoadingLayout extends RelativeLayout implements View.OnClickListener {

    private TextView failedMessage, noDataMessage, noDataAll, textViewNoData;
    private TextView     loadingTxt;
    private LinearLayout loadingLayout, failLayout, noDataLayout, noDataLayoutTwo;

    private ImageView noneReloadImage;

    private RefreshCallBackListener mRefreshCallBackListener;
    private NoDataCallBackListener  mNoDataCallBackListener;

    public LoadingLayout(Context context) {
        super(context);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findViewById(R.id.setNetwork).setOnClickListener(this);
        findViewById(R.id.relodingimag).setOnClickListener(this);
        failedMessage = (TextView) findViewById(R.id.failedMessage);
        loadingLayout = (LinearLayout) findViewById(R.id.loding_layout);
        noDataLayoutTwo = (LinearLayout) findViewById(R.id.nodata_layout);
        failLayout = (LinearLayout) findViewById(R.id.loding_faile);
        noDataLayout = (LinearLayout) findViewById(R.id.loading_none);
        noneReloadImage = (ImageView) findViewById(R.id.noneReloadImage);
        noDataMessage = (TextView) findViewById(R.id.noneMessage);
        noDataAll = (TextView) findViewById(R.id.setAll);
        loadingTxt = (TextView) findViewById(R.id.textView2);
        textViewNoData = (TextView) findViewById(R.id.textView_nodata);
        noDataAll.setOnClickListener(this);
        loadingLayout.setOnClickListener(null);
        failLayout.setOnClickListener(null);
        noDataLayout.setOnClickListener(null);
    }

    //设置第二种没用内容的文字显示
    public void setTextViewNoData(String text) {
        textViewNoData.setText(text);
    }

    //设置加载中提示语
    public void setLoadingHintMsg(String loadingMsg) {
        loadingTxt.setText(loadingMsg);
    }

    //设置没有数据时,可点击提示信息
    public void setNoDataAll(String msg) {
        noDataAll.setText(msg);
    }

    //设置没有数据时,提示信息
    public void setNoDataMsg(String msg) {
        noDataMessage.setText(msg);
    }

    //设置没有数据时，显示的图片
    public void setNoDataImage(int resId) {
        noneReloadImage.setImageResource(resId);
    }

    //设置
    public void isNoDataImage(int vis) {
        noneReloadImage.setVisibility(vis);

    }


    //设置没有数据时，显示的图片
    public void setNoDataImage(Bitmap mBitmap) {
        noneReloadImage.setImageBitmap(mBitmap);
    }

    //设置刷新回调
    public void setRefreshCallBackListener(RefreshCallBackListener mRefreshCallBackListener) {
        this.mRefreshCallBackListener = mRefreshCallBackListener;
    }

    //设置数据为0时，点击事件
    public void setNoDataCallBackListener(NoDataCallBackListener mNoDataCallBackListener) {
        this.mNoDataCallBackListener = mNoDataCallBackListener;
    }

    //设置加载失败文字
    public void setFailedMessage(String strMessage) {
        failedMessage.setText(strMessage);
    }

    //隐藏加载页面
    public void hideLoadingLayout() {
        loadingLayout.setVisibility(GONE);
        failLayout.setVisibility(GONE);
        noDataLayout.setVisibility(GONE);
        noDataLayoutTwo.setVisibility(GONE);
    }

    public boolean isHideLoadingLayout() {
        return loadingLayout.getVisibility() == GONE;
    }

    //显示没有数据第二种布局
    public void showNoDataLayoutTwo() {
        hideLoadingLayout();
        noDataLayoutTwo.setVisibility(VISIBLE);
    }


    //显示加载中
    public void showLoadingLayout() {
        hideLoadingLayout();
        loadingLayout.setVisibility(VISIBLE);
    }

    //显示加载失败
    public void showFailLayout() {
        hideLoadingLayout();
        failLayout.setVisibility(VISIBLE);
    }


    //显示加载数据为0
    public void showNoDataLayout() {
        hideLoadingLayout();
        noDataLayout.setVisibility(VISIBLE);
    }

    //设置加载中页面背景页面
    public void setLoadingLayoutBGColor(int color) {
        loadingLayout.setBackgroundColor(color);
    }


    //设置加载中页面背景透明度
    public void setLoadingLayoutBgAlpha(float alpha) {
        loadingLayout.setAlpha(alpha);
    }


    //设置加载失败页面背景透明度
    public void setFailLayoutBgAlpha(float alpha) {
        failLayout.setAlpha(alpha);
    }

    //设置上边距
    public void setLoadingLayoutMarginTop(int marginTop) {
        LayoutParams loadingLayoutLayoutParams   = (LayoutParams) loadingLayout.getLayoutParams();
        LayoutParams failLayoutLayoutParams      = (LayoutParams) failLayout.getLayoutParams();
        LayoutParams noDataLayoutLayoutParams    = (LayoutParams) failLayout.getLayoutParams();
        LayoutParams noDataLayoutTwoLayoutParams = (LayoutParams) failLayout.getLayoutParams();
        loadingLayoutLayoutParams.topMargin = marginTop;
        failLayoutLayoutParams.topMargin = marginTop;
        noDataLayoutLayoutParams.topMargin = marginTop;
        noDataLayoutTwoLayoutParams.topMargin = marginTop;
        loadingLayout.setLayoutParams(loadingLayoutLayoutParams);
        failLayout.setLayoutParams(failLayoutLayoutParams);
        noDataLayoutTwo.setLayoutParams(noDataLayoutTwoLayoutParams);
        noDataLayout.setLayoutParams(noDataLayoutLayoutParams);
    }


    //把loading，附加到activity上
    public void attachToActivity(Activity activity) {
        ViewGroup viewGroup   = (ViewGroup) activity.findViewById(android.R.id.content);
        View      contentView = viewGroup.getChildAt(0);
        viewGroup.removeView(contentView);
        addView(contentView, 0);
        viewGroup.addView(this);
    }

    //把loading，附加到activity上
    public void attachView(View view, int childId) {
        try {
            View      contentView = view.findViewById(childId);
            ViewGroup viewGroup   = (ViewGroup) contentView.getParent();
            if (viewGroup == null) {
                throw new Exception("the view must has a parent");
            }
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                if (child.getId() == childId) {
                    viewGroup.removeView(contentView);
                    viewGroup.addView(this, i, contentView.getLayoutParams());
                    break;
                }
            }
            addView(contentView, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //把loading，附加到fragment上
    public void attachViewForFragment(View view, int childId) {
        try {
            View      contentView = view.findViewById(childId);
            ViewGroup viewGroup   = (ViewGroup) contentView.getParent();
            if (viewGroup == null) {
                addView(contentView, 0);
                return;
            }
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                if (child.getId() == childId) {
                    viewGroup.removeView(contentView);
                    viewGroup.addView(this, i, contentView.getLayoutParams());
                    break;
                }
            }
            addView(contentView, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setNetwork:

                Intent intent = null;
                //判断手机系统的版本  即API大于10 就是3.0或以上版本
//        if (android.os.Build.VERSION.SDK_INT > 8) {
//            intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
//        } else {
//            intent = new Intent();
//            ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
//            intent.setComponent(component);
//            intent.setAction("android.intent.action.VIEW");
//        }
                intent = new Intent(Settings.ACTION_SETTINGS);
                getContext().startActivity(intent);
                break;
            case R.id.relodingimag:
                if (mRefreshCallBackListener != null) {
                    mRefreshCallBackListener.callBack();
                }
                break;
            case R.id.setAll:
                if (mNoDataCallBackListener != null) {
                    mNoDataCallBackListener.noDataCallBack();
                }
                break;
        }
    }


    //刷新回调
    public interface RefreshCallBackListener {
        void callBack();
    }

    public interface NoDataCallBackListener {
        void noDataCallBack();
    }

}
